package com.example.firstboot.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.firstboot.entity.Mail;
import com.example.firstboot.entity.TbUser;
import com.example.firstboot.service.TbUserService;
import com.example.firstboot.util.JsonResult;
import com.example.firstboot.util.MD5Utils;
import com.example.firstboot.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class MailController {

    @Autowired
    private Mail mail;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private MailUtils mailUtils;

//    @RequestMapping(value = "/gateway")
//    public String gateway() {
//        System.out.println(mail.getCode());
//        return mail.getCode();
//    }

    @RequestMapping("/testMail")
    public void testMail() throws Exception {
        mailUtils.send("2960787865@qq.com","test","有头");
    }

//    public static void main(String[] args) throws Exception {
//        MailUtils mailUtils1 = new MailUtils();
//        for (int i = 0; i < 50; i++) {
//            int code = mailUtils1.sendVerificationCode("279524727@qq.com");
//        }
//    }


    @RequestMapping("/resetPassword")
    public JsonResult testMail1(@RequestBody JSONObject jsonObject) throws Exception {
        String email = String.valueOf(jsonObject.get("email"));
        String account = String.valueOf(jsonObject.get("account"));
        Integer password=mailUtils.sendPassword(email);
        Integer row = tbUserService.resetPasswordByAccount(account, MD5Utils.getMD5(String.valueOf(password)));

        if (row ==0) {
            return new JsonResult(-1, "修改密码失败");
        }else {
            return new JsonResult(0, "修改密码成功");
        }
    }
}
